﻿package mortal.game.manager
{
    import com.gengine.global.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import mortal.component.window.*;
    import mortal.mvc.interfaces.*;

    public class PopupManager extends Object
    {
        private static var _topDisplayObject:DisplayObject;

        public function PopupManager()
        {
            return;
        }// end function

        public static function addPopUp(param1:DisplayObject, param2:String = "window", param3:Boolean = false) : void
        {
            if (param1 && LayerManager.windowLayer.contains(param1) == false)
            {
                param1.x = (Global.stage.stageWidth - param1.width) / 2;
                param1.y = (Global.stage.stageHeight - param1.height) / 2;
                if (param2 == "window")
                {
                    LayerManager.windowLayer.addChild(param1);
                }
                else if (param1 is IView)
                {
                    (param1 as IView).show();
                }
                _topDisplayObject = param1;
                if (param3)
                {
                    createTempCover(param1, 0, 0, true);
                }
            }
            return;
        }// end function

        public static function centerPopup(param1:DisplayObject) : void
        {
            if (param1)
            {
                param1.x = (Global.stage.stageWidth - param1.width) / 2;
                param1.y = (Global.stage.stageHeight - param1.height) / 2;
            }
            return;
        }// end function

        public static function isTop(param1:DisplayObject) : Boolean
        {
            return LayerManager.windowLayer.isTop(param1);
        }// end function

        public static function removePopup(param1:DisplayObject) : void
        {
            if (LayerManager.windowLayer.contains(param1))
            {
                LayerManager.windowLayer.removeChild(param1);
                if (_topDisplayObject == param1)
                {
                    _topDisplayObject = null;
                }
            }
            return;
        }// end function

        public static function isPopup(param1:DisplayObject) : Boolean
        {
            return LayerManager.windowLayer.contains(param1);
        }// end function

        public static function setTop(param1:Window) : void
        {
            _topDisplayObject = param1;
            param1.layer.setTop(param1);
            return;
        }// end function

        private static function createTempCover(param1:DisplayObject, param2:uint = 0, param3:Number = 0.5, param4:Boolean = false) : void
        {
            var parent:DisplayObjectContainer;
            var back:Sprite;
            var removeHandler:Function;
            var v:* = param1;
            var color:* = param2;
            var alpha:* = param3;
            var mouseEnabled:* = param4;
            removeHandler = function (event:Event) : void
            {
                parent.removeEventListener(Event.REMOVED_FROM_STAGE, removeHandler);
                if (parent.contains(back))
                {
                    parent.removeChild(back);
                }
                return;
            }// end function
            ;
            parent = v.parent;
            back = new Sprite();
            var rect:* = localRectToContent(new Rectangle(0, 0, parent.stage.stageWidth, parent.stage.stageHeight), parent.stage, parent);
            back.graphics.beginFill(color, alpha);
            back.graphics.drawRect(rect.x, rect.y, rect.width, rect.height);
            back.graphics.endFill();
            back.mouseEnabled = mouseEnabled;
            back.cacheAsBitmap = true;
            parent.addChildAt(back, parent.getChildIndex(v));
            v.addEventListener(Event.REMOVED_FROM_STAGE, removeHandler);
            return;
        }// end function

        private static function localRectToContent(param1:Rectangle, param2:DisplayObject, param3:DisplayObject) : Rectangle
        {
            if (param2 == param3)
            {
                return param1;
            }
            var _loc_4:* = localToContent(param1.topLeft, param2, param3);
            var _loc_5:* = localToContent(param1.bottomRight, param2, param3);
            return new Rectangle(_loc_4.x, _loc_4.y, _loc_5.x - _loc_4.x, _loc_5.y - _loc_4.y);
        }// end function

        public static function localToContent(param1:Point, param2:DisplayObject, param3:DisplayObject) : Point
        {
            if (param3 && param2)
            {
                return param3.globalToLocal(param2.localToGlobal(param1));
            }
            if (param2)
            {
                return param2.localToGlobal(param1);
            }
            if (param3)
            {
                return param3.globalToLocal(param1);
            }
            return null;
        }// end function

        public static function closeAllFullView() : Boolean
        {
            LayerManager.fullScreenLayer.removeAllWindow();
            LayerManager.fullScreenTopLayer.removeAllWindow();
            return true;
        }// end function

        public static function closeTopWindow() : Boolean
        {
            var _loc_3:int = 0;
            var _loc_4:IView = null;
            var _loc_1:* = LayerManager.topWindowLayer;
            var _loc_2:* = _loc_1.numChildren;
            _loc_3 = _loc_2 - 1;
            while (_loc_3 >= 0)
            {
                
                _loc_4 = _loc_1.getChildAt(_loc_3) as IView;
                if (_loc_4)
                {
                    _loc_4.hide();
                    return true;
                }
                _loc_3 = _loc_3 - 1;
            }
            _loc_1 = LayerManager.bottomWindowLayer;
            _loc_2 = _loc_1.numChildren;
            _loc_3 = _loc_2 - 1;
            while (_loc_3 >= 0)
            {
                
                _loc_4 = _loc_1.getChildAt(_loc_3) as IView;
                if (_loc_4)
                {
                    _loc_4.hide();
                    return true;
                }
                _loc_3 = _loc_3 - 1;
            }
            return false;
        }// end function

    }
}
